<?php
class Core_Model_DbTable_UserActions extends Zend_Db_Table_Abstract {
    protected $_name = 'user_actions';
    
    public function getActions($id_social_page, 
                               $id_social_user, 
                               $start_time,
                               $end_time,
                               $action_type = NULL, 
                               $offet       = 0, 
                               $count       = ACTION_PER_PAGE) 
    {
        $query = $this->select()
                      ->from($this->_name)
                      ->where("id_social_page = '$id_social_page'")
                      ->where("id_social_user = '$id_social_user'")
                      ->where("created_time BETWEEN $start_time AND $end_time")
                      ->limit($count, $offet);
        if (!empty($action_type)) {
            $query->where("type = '$action_type'");
        }
        return $this->fetchAll($query);
    }
    
    public function insertActions($action_details, $action_type) {
        if (!empty($action_details)) {  
            $translate_cols = $this->getTranslateCols($action_type);
            $str_cols = $this->getStrCols();
            $sql = "INSERT DELAYED IGNORE INTO {$this->_name} ($str_cols) VALUES ";
            foreach ($action_details as $action_detail) {
                $data = array();
                foreach ($translate_cols as $translated_col => $col) {
                    if (isset($action_detail[$translated_col])) {
                        $data[$col] = $action_detail[$translated_col];
                    } else {
                        $data[$col] = NULL;
                    }
                }
                $data['type'] = $action_type;
                $val  = $this->getAdapter()->quoteInto("(?),", $data);
                $sql .= $val;
            }
            $final_sql = rtrim($sql, ",");
            $this->getAdapter()->query($final_sql);
        }
    }
    
    public function getCols() {
        return array (
            'id_social_page',
            'id_social_user',
            'id_social_action',
            'action_message',
            'id_social_target',
            'target_message',
            'picture',
            'target_id_social_from',
            'target_created_time',
            'created_time',
            'type'
        );
    }
    
    protected function getTranslateCols($type) {
        $result = NULL;
        switch ($type) {
            case COMMENT:
            case LIKE:
                $result = array (
                            'id_social_page'        => 'id_social_page',
                            'id_social_user'        => 'id_social_user',
                            'id_social_comment'     => 'id_social_action',
                            'message'               => 'action_message',
                            'id_social_post'        => 'id_social_target',
                            'target_message'        => 'target_message',
                            'picture'               => 'picture',
                            'target_id_social_from' => 'target_id_social_from',
                            'target_created_time'   => 'target_created_time',                
                            'created_time'          => 'created_time'
                          );  
                break;
            case POST:
                $result = array (
                            'to'                    => 'id_social_page',
                            'from'                  => 'id_social_user',
                            'id_social_post'        => 'id_social_action',
                            'message'               => 'action_message',    
                            'id_social_target'      => 'id_social_target',
                            'target_message'        => 'target_message',
                            'picture'               => 'picture',
                            'target_id_social_from' => 'target_id_social_from',
                            'target_created_time'   => 'target_created_time',
                            'created_time'          => 'created_time'
                          );
                break; 
        }
        return $result;
    }
    
    public function getStrCols() {
        $cols = $this->getCols();
        return $this->colToStr($cols);
    }
    
    public function colToStr($cols) {
        $result = "";
        foreach ($cols as $col) {
            $result .= "`$col`,";
        }
        return rtrim($result, ',');
    }
    
    public function checkVoted($id_social_page, $id_social_user, $voted_ids, $start_time, $end_time) {
        if (empty($voted_ids)) {
            return;
        }
        if (empty($id_social_user)) {
            foreach ($voted_ids as $voted_id) {
                $result[$voted_id] = true;
            }
        } else {
            $query = $this->select()
                          ->from($this->_name, array('id', 'id_social_user'))
                          ->where("id_social_page = '$id_social_page'")
                          ->where("id_social_user IN (?)", $voted_ids)
                          ->where("type = ?", VOTE)
                          ->where("target_id_social_from = '$id_social_user'")
                          ->where("created_time BETWEEN $start_time AND $end_time");
            
            $votes = $this->fetchAll($query)->toArray();
            $result = array();
            foreach ($voted_ids as $voted_id) {
                $result[$voted_id] = 0;
                foreach ($votes as $vote) {
                    if ($vote['id_social_user'] == $voted_id) {
                        $result[$voted_id] = $vote['id'];
                        break;
                    }
                }
            }
        }
        return $result;
    }
}
?>
